<?php

namespace app\controller;

use app\BaseController;
use app\model\AppFun;
use app\model\SysAccount;
use app\model\SysDepartment\SysAccountDepart;
use app\model\SysDepartment\SysDepartmentRole;
use app\utils\CamlHump;
use app\utils\Lodash;
use app\utils\RedisUtils;
use app\utils\ResposeUtils;
use think\facade\Log;
use think\facade\Request;
use Underscore\Types\Arrays;

class AuthController extends BaseController
{
    public function  getuserinfobyauth()
    {
        $tokenstr = Request::header('Authorization');
        $token=str_replace('Bearer ','',$tokenstr);
        $userId = RedisUtils::getStr('login:'.$token);
        if(!isset($userId)){
            return ResposeUtils::failRes("没有登录信息");
        }
        $userInfo = SysAccount::findOne($userId);
        unset($userInfo['passWord']);
        return ResposeUtils::ok([
            '$userInfo' => $userInfo,
        ]);
    }
    public function  unlogin()
    {
        $tokenstr = Request::header('Authorization');
        $token=str_replace('Bearer ','',$tokenstr);
         RedisUtils::del('login:'.$token);
        return ResposeUtils::ok([]);
    }

    public function login()
    {
        $body = Request::post();

        if (!isset($body['username'])) {
            return ResposeUtils::failRes("请输入username");
        }
        if (!isset($body['password'])) {
            return ResposeUtils::failRes("请输入password");
        }


        $body['userName'] = $body['username'];
        $body['passWord'] = $body['password'];
        //获取用户
        $query = SysAccount::createQuery($body);
        $one = CamlHump::toCamlObj($query->find());
        if (!isset($one['id'])) {
            return ResposeUtils::failRes("没有" . $body['username'] . "用户");
        }

        //设置token到redis
        $redisToken=Lodash::getRand();
        RedisUtils::setStr("login:".$redisToken,$one['id']);

        //获取到 返回成功 返回一些基本信息
        unset($one['passWord']);
        return ResposeUtils::ok([
            "token" => $redisToken,
            "authInfo" => $one
        ]);
    }

    public function getmenu()
    {
        $body = Request::post();
        $tokenHeader = Request::header('Authorization');
        $token=str_replace('Bearer ','',$tokenHeader);

        $userId=RedisUtils::getStr("login:".$token);
        if(!isset($userId)){
            return ResposeUtils::ok(
                []
            );
        }
        //找部门
        $departList = CamlHump::toCamlArray(SysAccountDepart::createQuery(["accountId" => $userId])->select());
        //找角色
        $roleIds=[];
        Lodash::ForEach($departList,function($departItem) use (&$roleIds) {
            $departId = $departItem['departId'];
            $tempDepartRoleList = CamlHump::toCamlArray(SysDepartmentRole::createQuery([
                'departId' => $departId
            ])->select());

            array_push($roleIds,...Lodash::Map($tempDepartRoleList,function ($item) {
                return $item['roleId'];
            }));
        });

        //找菜单
        $funIds = [];
        Lodash::ForEach($roleIds,function ($roleId) use (&$funIds) {
            $temRoleFunIds = CamlHump::toCamlArray(SysAccount\SysRoleFun::createQuery([
                "roleId" => $roleId,
            ])->select());
            Lodash::ForEach($temRoleFunIds,function ($temRoleFunId) use (&$funIds) {
                if(isset($temRoleFunId['funId'])&&$temRoleFunId['funId']>0){
                    array_push($funIds,$temRoleFunId['funId']);
                }
            });
        });

        //加入他们的父类id
        $parentIds = [];
        Lodash::map($funIds,function ($funId) use (&$parentIds) {
            $menuone=AppFun::findOne($funId);
            if(isset($menuone)&&isset($menuone['parentIdPath'])){
                $parentIdPathArray=explode(',', $menuone['parentIdPath']);
                array_push($parentIds, ...$parentIdPathArray);
            }
        });
        array_push($funIds, ...$parentIds);





        //获取 app_fun  加入in 只能查找我自己可以看到的菜单范围 funIds
            $query=AppFun::createQuery($body);
            if(isset($body['funTypes'])){
                $query->where("fun_type",'in',$body['funTypes']);
            }
          if(!empty($funIds)){
                    $query->where("id",'in',$funIds);
           }
         $templist=  CamlHump::toCamlArray($query->select());




          //for 加入字段 haveNext
            Lodash::ForEach($templist,function (&$item){
                $item['haveNext'] = AppFun::haveNext($item['id']);
            });


            //获取到 返回成功
            return ResposeUtils::ok(
                $templist
            );
        }

}
